import 'package:flutter/material.dart';
import 'package:flutter/services.dart';
import 'package:get/get.dart';
import 'package:smart_devices/bruno/src/components/button/brn_normal_button.dart';
import 'package:smart_devices/bruno/src/components/radio/brn_radio_button.dart';
import 'package:smart_devices/bruno/src/components/toast/brn_toast.dart';
import 'package:smart_devices/utils/colors.dart';
import 'package:smart_devices/utils/styles.dart';
import 'package:smart_devices/utils/tools.dart';
import 'package:smart_devices/vo/device_supplier.dart';

import 'supplier_logic.dart';

class DeviceSupplierView extends GetView<DeviceSupplierLogic> {
  const DeviceSupplierView({Key? key}) : super(key: key);

  @override
  Widget build(BuildContext context) {
    return GetBuilder<DeviceSupplierLogic>(
      builder: (_) {
        return Scaffold(
          backgroundColor:
              ColorsUtils.parseColorFromHexString(ColorsUtils.colorFfff),
          appBar: AppBar(
            leading: IconButton(
              icon: const Icon(
                Icons.arrow_back_ios,
                color: Colors.white,
                size: 15,
              ),
              onPressed: Get.back,
            ),
            automaticallyImplyLeading: false,
            backgroundColor:
                ColorsUtils.parseColorFromHexString(ColorsUtils.color0052),
            title: Text(
              '供应商',
              style: StylesUtils.customTextStyle(
                  fontSize: 18, fontWeight: FontWeight.w700),
            ),
            centerTitle: true,
            systemOverlayStyle: SystemUiOverlayStyle.dark,
            bottom: PreferredSize(
                preferredSize: const Size.fromHeight(50),
                child: Container(
                  height: 50,
                  padding:
                      const EdgeInsets.symmetric(horizontal: 15, vertical: 7),
                  decoration: const BoxDecoration(color: Colors.white),
                  child: Container(
                    decoration: BoxDecoration(
                        borderRadius: BorderRadius.circular(5),
                        border: Border.all(
                            color: ColorsUtils.parseColorFromHexString(
                                ColorsUtils.colorE4e4))),
                    child: Row(
                      children: [
                        Container(
                          width: 36,
                          alignment: Alignment.center,
                          child: ToolsUtils.urlSvg(
                              'https://cdn7.axureshop.com/demo2023/2163241/images/常用组件/u2227.svg'),
                        ),
                        Expanded(
                            child: TextField(
                          textDirection: TextDirection.ltr,
                          controller: TextEditingController(
                              text: controller.search.value),
                          cursorColor: ColorsUtils.parseColorFromHexString(
                              ColorsUtils.color1c24),
                          style: StylesUtils.customTextStyle(
                              fontSize: 16, color: ColorsUtils.color1c24),
                          textAlign: TextAlign.left,
                          textAlignVertical: TextAlignVertical.center,
                          cursorWidth: 2,
                          // cursorHeight: 18,
                          cursorRadius: const Radius.circular(2),
                          decoration: InputDecoration(
                              hintText: '请输入订单号或采购主题',
                              hintStyle: StylesUtils.customTextStyle(
                                  color: ColorsUtils.colorCccc, fontSize: 13),
                              isDense: true,
                              isCollapsed: true,
                              border: InputBorder.none,
                              focusColor: ColorsUtils.parseColorFromHexString(
                                  ColorsUtils.color1c24),
                              contentPadding:
                                  const EdgeInsets.symmetric(vertical: 13),
                              focusedBorder: InputBorder.none),
                          onChanged: (String text) {
                            controller.search.value = text;
                          },
                        ))
                      ],
                    ),
                  ),
                )),
            elevation: 0,
          ),
          body: Obx(() => SingleChildScrollView(
                padding: const EdgeInsets.only(
                    left: 10, right: 10, top: 10, bottom: 80),
                child: Table(
                  defaultVerticalAlignment: TableCellVerticalAlignment.middle,
                  columnWidths: const <int, TableColumnWidth>{
                    0: FixedColumnWidth(155),
                    1: FlexColumnWidth(),
                    2: FlexColumnWidth(),
                  },
                  border: TableBorder(
                      top: BorderSide(
                          color: ColorsUtils.parseColorFromHexString(
                              ColorsUtils.colorD7d7)),
                      left: BorderSide(
                          color: ColorsUtils.parseColorFromHexString(
                              ColorsUtils.colorD7d7)),
                      right: BorderSide(
                          color: ColorsUtils.parseColorFromHexString(
                              ColorsUtils.colorD7d7))),
                  children: appendTableRow(),
                ),
              )),
          extendBody: true,
          bottomSheet: Container(
            height: 70,
            decoration: BoxDecoration(
                color: Colors.white,
                border: Border(
                    top: BorderSide(
                        color: ColorsUtils.parseColorFromHexString(
                            ColorsUtils.colorE4e4)))),
            alignment: Alignment.center,
            child: FractionallySizedBox(
              widthFactor: 0.9,
              heightFactor: 0.75,
              alignment: Alignment.center,
              child: BrnNormalButton(
                onTap: () {
                  DeviceSupplierVo? supplier = controller.suppliers
                      .firstWhereOrNull((element) => element.checked);
                  if (supplier != null) {
                    return Get.back(result: supplier);
                  }
                  BrnToast.show('请选择采购订单', context,
                      gravity: BrnToastGravity.bottom);
                },
                backgroundColor:
                    ColorsUtils.parseColorFromHexString(ColorsUtils.color0052),
                text: '',
                child: Text(
                  '提交',
                  style: StylesUtils.customTextStyle(fontSize: 14),
                ),
              ),
            ),
          ),
        );
      },
      init: controller,
    );
  }

  List<TableRow> appendTableRow() {
    List<TableRow> rows = [];
    rows.add(TableRow(
        decoration: BoxDecoration(
            border: Border(
                bottom: BorderSide(
                    color: ColorsUtils.parseColorFromHexString(
                        ColorsUtils.colorD7d7))),
            color: ColorsUtils.parseColorFromHexString(ColorsUtils.colorF3f3)),
        children: <TableCell>[
          TableCell(
              child: Container(
            height: 40,
            alignment: Alignment.center,
            child: Text(
              '供应商名称',
              style: StylesUtils.customTextStyle(color: ColorsUtils.color4950),
            ),
          )),
          TableCell(
              child: Center(
            child: Text(
              '联系人',
              style: StylesUtils.customTextStyle(color: ColorsUtils.color4950),
            ),
          )),
          TableCell(
              child: Center(
            child: Text(
              '联系电话',
              style: StylesUtils.customTextStyle(color: ColorsUtils.color4950),
            ),
          )),
        ]));
    for (var i = 0; i < controller.suppliers.length; i++) {
      rows.add(TableRow(
          decoration: BoxDecoration(
              color: i % 2 == 0
                  ? Colors.white
                  : ColorsUtils.parseColorFromHexString(ColorsUtils.colorF3f3),
              border: Border(
                  bottom: BorderSide(
                      color: ColorsUtils.parseColorFromHexString(
                          ColorsUtils.colorD7d7)))),
          children: <TableCell>[
            TableCell(
                child: Container(
              height: 40,
              alignment: Alignment.center,
              child: Wrap(
                spacing: 15,
                children: [
                  BrnRadioButton(
                    radioIndex: i,
                    onValueChangedAtIndex: (int index, bool flag) {
                      for (var o in controller.suppliers) {
                        o.checked = false;
                      }
                      controller.suppliers[index].checked = flag;
                      controller.suppliers.refresh();
                    },
                    isSelected: controller.suppliers[i].checked,
                    iconPadding: EdgeInsets.zero,
                    mainAxisAlignment: MainAxisAlignment.center,
                  ),
                  Text(
                    controller.suppliers[i].name,
                    overflow: TextOverflow.ellipsis,
                    style: StylesUtils.customTextStyle(
                        color: ColorsUtils.color3333),
                  ),
                ],
              ),
            )),
            TableCell(
                child: Center(
              child: Text(
                controller.suppliers[i].contact,
                style:
                    StylesUtils.customTextStyle(color: ColorsUtils.color3333),
              ),
            )),
            TableCell(
                child: Center(
              child: Text(
                controller.suppliers[i].mobile,
                style:
                    StylesUtils.customTextStyle(color: ColorsUtils.color3333),
              ),
            ))
          ]));
    }
    return rows;
  }
}
